package com.bana.sys.controller;

import com.bana.sys.code.DatabaseResult;
import com.bana.sys.dao.DepartmentDAO;
import com.bana.sys.model.Department;
import com.bana.sys.service.DepartmentService;
import com.bana.sys.utils.ActionResult;
import com.bana.sys.utils.TokenRequest;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController
@RequestMapping(value = "/department",method = RequestMethod.POST)
public class DepartmentController {

    @Resource
    private DepartmentDAO departmentDAO;

    @Resource
    private DepartmentService departmentService;

    /**
     * 查询部门集合
     * @param request
     * @return
     */
    @RequestMapping(value = "getDepartmentTree.do",method = RequestMethod.POST)
    public Department getDepartmentTree(@RequestBody TokenRequest<Object> request){

        return departmentService.getDepartmentTree(request.getToken());
    }

    @RequestMapping(value = "insertDepartment.do",method = RequestMethod.POST)
    public ActionResult insertDeparment(@RequestBody TokenRequest<Department> request){
        departmentDAO.insertDepartment(request.getData());
        ActionResult actionResult=new ActionResult();
        actionResult.setCzjg(DatabaseResult.Success);
        actionResult.setKey(request.getData().getJgbm());
        return actionResult;
    }

    @RequestMapping(value = "updateDepartment.do",method = RequestMethod.POST)
    public ActionResult updateDepartment(@RequestBody TokenRequest<Department> request){
        departmentDAO.updateDepartment(request.getData());
        ActionResult actionResult=new ActionResult();
        actionResult.setCzjg(DatabaseResult.Success);
        return actionResult;
    }

    @RequestMapping(value = "deleteDepartment.do",method = RequestMethod.POST)
    public ActionResult deleteDepartment(@RequestBody TokenRequest<Department> request){
        departmentService.deleteDepartment(request.getData());
        ActionResult actionResult=new ActionResult();
        actionResult.setCzjg(DatabaseResult.Success);
        return actionResult;
    }

}
